package ca.usherbrooke.domus.sipnmove.call.view.impl;

import ca.usherbrooke.domus.sipnmove.call.Call;
import ca.usherbrooke.domus.sipnmove.call.impl.SipCall;
import ca.usherbrooke.domus.sipnmove.call.impl.SocketCall;
import ca.usherbrooke.domus.sipnmove.call.view.CallView;
import ca.usherbrooke.domus.sipnmove.call.view.CallViewFactory;

/**
 * Create SIP/Socket call view from a SIP/Socket call object
 */
public class SipSocketCallViewFactory implements CallViewFactory {

	@Override
	public CallView createCallView(Call call) {
		
		if(!(call instanceof SipCall))
			System.err.println("Internal error : need a SipCall instance in SipSocketCallViewFactory");

		SipCall sipCall = (SipCall) call;
		SocketCall socketCall = (SocketCall) sipCall.getDecoratedCall();

		SipSocketCallView callView = new SipSocketCallView();

		// Create send message action controller
		SendSocketMessageActionController sendSocketMessageActionController = new SendSocketMessageActionController();
		sendSocketMessageActionController.setSipSocketCallView(callView);
		sendSocketMessageActionController.setSocketCall(socketCall);

		// Create terminate call action controller
		TerminateCallActionController terminateCallActionController = new TerminateCallActionController();
		terminateCallActionController.setCall(call);

		callView.setSendMessageActionController(sendSocketMessageActionController);
		callView.setTerminateCallActionController(terminateCallActionController);

		// Register call view as call listener for both SIP and socket calls
		sipCall.addSipCallListener(callView);
		socketCall.addSocketCallListener(callView);
		
		return (CallView) callView;
	}

}
